package com.umeox.watch.moto.launcher.utils

import com.umeox.moto.common.log.Logger
import com.umeox.watch.moto.dataservice.DataProvider
import com.umeox.watch.moto.launcher.R
import com.umeox.watch.moto.launcher.bean.PrayerTimeInfo
import com.umeox.watch.moto.launcher.utils.string
import java.text.SimpleDateFormat
import java.util.*
import java.util.Calendar.*

class PrayerTimesCoordinator {

    companion object {
        private const val AM = 0
        private const val PM = 1
    }

    private val formatYMDHMS = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH)

    private var prayers: PrayTime2 = PrayTime2()
    private var genre = 0

    fun updateParameters(is24timeFormat: Boolean) {
        prayers.timeFormat = if (is24timeFormat) {
            prayers.time24
        } else {
            prayers.time12
        }

        val conventionIndex = DataProvider.getPrayerIndex()
        if (conventionIndex >= 100) {
            val contentValues = DataProvider.getPrayerSettingsCustomParams()
            val params: DoubleArray = doubleArrayOf(
                contentValues.getAsDouble(DataProvider.PrayerSettingsProvider.FAJR_ANGLE),
                contentValues.getAsInteger(DataProvider.PrayerSettingsProvider.MAGHRIB_SELECTOR)
                    .toDouble(),
                contentValues.getAsDouble(DataProvider.PrayerSettingsProvider.MAGHRIB_VALUE),
                contentValues.getAsInteger(DataProvider.PrayerSettingsProvider.ISHA_SELECTOR)
                    .toDouble(),
                contentValues.getAsDouble(DataProvider.PrayerSettingsProvider.ISHA_VALUE),
            )
            Logger.e("updateParameters", params.contentToString())
            prayers.setCustomParams(params)
        } else {
            prayers.calcMethod = when (conventionIndex) {
                0 -> prayers.mWL
                1 -> prayers.iSNA
                2 -> prayers.egypt
                3 -> prayers.makkah
                4 -> prayers.karachi
                5 -> prayers.tehran
                6 -> prayers.jafari
                7 -> prayers.muis
                8 -> prayers.jakim
                9 -> prayers.kemenag
                10 -> prayers.diyanet
                11 -> prayers.dsmr
                12 -> prayers.dumsr
                13 -> prayers.gaiae
                14 -> prayers.algeria
                15 -> prayers.uoif
                16 -> prayers.france15
                17 -> prayers.france18
                18 -> prayers.tunisia
                19 -> prayers.london
                20 -> prayers.gulf
                21 -> prayers.ingushetia
                22 -> prayers.brunei_kheu
                else -> prayers.tehran
            }
        }

        genre = DataProvider.getAsrJuristicMethod()
        prayers.asrJuristic = when (genre) {
            0 -> prayers.shafii
            1 -> prayers.hanafi
            else -> prayers.hanafi
        }

        prayers.adjustHighLats = prayers.angleBased

        val offsets =
            intArrayOf(0, 0, 0, 0, 0, 0, 0)
        prayers.tune(offsets)
    }

    /**
     * 根据相关参数计算各时刻的时间点
     */
    fun getPrayerTimeData(
        latitude: Double,
        longitude: Double,
        timeZoneOffSet: Double,
    ): MutableList<PrayerTimeInfo> {
        val cal = getInstance()
        cal.time = Date()

        val prayerTimeList = prayers.getDatePrayerTimes(
            cal[YEAR], cal[MONTH] + 1, cal[DATE],
            latitude, longitude, timeZoneOffSet, genre
        )

        Logger.e("$prayerTimeList")

        val prayerNameList = prayers.timeNames
        val list = mutableListOf<PrayerTimeInfo>()

        for (i in prayerTimeList.indices) {
            val timeType = if (prayerTimeList[i].endsWith("AM")) {
                AM
            } else {
                PM
            }

            val time = if (i == 7) {
                "00:00"
            } else {
                prayerTimeList[i].substring(0, prayerTimeList[i].length - 2)
            }
            val timestamp = if (i == 7) {
                val instance = getInstance()
                instance.add(DAY_OF_MONTH, 1)
                val year = instance[YEAR]
                val month = instance[MONTH] + 1
                val day = instance[DAY_OF_MONTH]
                convertDateTimeStrToTimestamp(
                    "$year-${
                        String.format(
                            Locale.ENGLISH,
                            "%02d",
                            month
                        )
                    }-${String.format(Locale.ENGLISH, "%02d", day)} 00:00:00", formatYMDHMS
                )
            } else {
                convertDateTimeStrToTimestamp(
                    "${
                        getCurrentDateTimeStr(
                            SimpleDateFormat(
                                "yyyy-MM-dd",
                                Locale.CHINA
                            )
                        )
                    } $time:00", formatYMDHMS
                )
            }

            if (i != 4) {
                val prayerTimeInfo = PrayerTimeInfo(
                    if (i > 4) i - 1 else i,
                    time,
                    timestamp,
                    if (timeType == AM) R.string.common_am.string else R.string.common_pm.string,
                    prayerNameList[i].string,
                    false
                )
                list.add(prayerTimeInfo)
            }
        }
        return list
    }

    /**
     * 转换 日期时间字符 为 时间戳
     */
    private fun convertDateTimeStrToTimestamp(
        dateTimeStr: String,
        simpleDateFormat: SimpleDateFormat
    ): Long {
        val dateTime = simpleDateFormat.parse(dateTimeStr)
        return dateTime?.time ?: 0L
    }

    /**
     * 获取当前时间日期的指定格式的字符串
     * 如：SimpleDateFormat("yyyy-MM-dd",Locale.CHINA)
     * 得到的结果就是  “2022-06-24”
     */
    private fun getCurrentDateTimeStr(simpleDateFormat: SimpleDateFormat): String {
        return simpleDateFormat.format(Date())
    }

}
